<template>
  <view class="photo" :style="[{ backgroundColor: color }]">
    <cu-custom :bgColor="'bgcolorfff'" :isBack="true">
      <block slot="content" class="toptext">
        <view>{{ allData.title }}</view>
      </block>
    </cu-custom>

    <!-- 顶部轮播图 -->
    <view class="top-img" :style="'height:' + swiperHeight + 'rpx'">
      <swiper
        class="swiper"
        :style="[{ height: swiperHeight + 'rpx' }]"
        :current="currentIndex"
        @change="swiperChange"
        :lazy-load="true"
        :circular="true"
      >
        <swiper-item
          v-for="(item, index) in swiperimgArr"
          :key="index"
          class="swiper-item"
        >
          <image
            :src="item.fileUrl"
            alt
            class="swiper-img"
            mode="widthFix"
            @load="imgload($event, index)"
            @click.stop="preview(item, index)"
          />
        </swiper-item>
      </swiper>

      <view
        class="dot"
        :style="[{ left: left }, { top: swiperHeight - 112 + 'rpx' }]"
      >
        <text>{{ currentIndex + 1 }}/{{ swiperimgArr.length }}</text>
      </view>
    </view>

    <!-- 主文章区域 -->
    <view class="article">
      <text class="article-title">{{ swiperimgArr[currentIndex].name }}</text>
      <view class="article-icon">
        <image :src="allData.category.icon" mode="widthFix" class="tuji" />
        <text>{{ allData.category.name }}</text>
      </view>
      <text class="article-text">{{ swiperimgArr[currentIndex].intro }}</text>
    </view>

    <!-- 评论缩小版 -->
    <view class="dis">
      <view class="discuss">
        <view class="discuss_list" @click="openComlist">
          <view v-for="(item, index) in previewList" :key="index">
            <text>{{ item.userName }}:</text>
            <text class="discuss_list_margin">{{ item.intro }}</text>
          </view>
        </view>
        <view class="speakEvery" @click="switchIn = false">说点什么...</view>
      </view>
    </view>

    <!-- 底部图文推荐列表 -->
    <view class="recommend" v-if="list.length > 0">
      <text class="recommend-title">相关图文推荐</text>
      <!-- 瀑布流卡片 -->
      <view class="waterfall_box">
        <waterfall :value="list" ref="waterfall" :column="2" :gutter="0">
          <template v-slot="{ item }">
            <view class="waterfall_box_cen">
              <Pullcard :item="item" :boxBorder="true" :showLikeIcon="false"
                >自定义模块</Pullcard
              >
            </view>
          </template>
        </waterfall>
        <view class="kongTab"></view>
      </view>
    </view>
    <view class="kongTab"></view>
    <!-- 底部点赞兰 -->
    <evaluate
      v-model="switchIn"
      :height="safeArea"
      @likeArtice="likeArtice"
      :likeIndex="likeArticeValue"
      :evaluateState="evaluateState"
      @openCommlist="offList = true"
    ></evaluate>
    <!-- 弹出的真实的输入框 -->
    <reallyInput
      @down="down"
      :switchIn="switchIn"
      v-model="reallyValue"
      @submite="submite"
    ></reallyInput>

    <!-- 评论详细列表 -->
    <commentList
      v-model="offList"
      :AllSC="safeArea"
      ref="commentList"
      :discussList="discussList"
      :hasChart="hasChart"
      @scrollBottom="scrollBottom"
    ></commentList>
    <image
      :src="
        'https://static.77du.net/template/ciff/img/hartgif.gif?id=' +
        Math.random()
      "
      @click.stop
      v-if="showLIkeGif"
      class="showLikeGift"
      style="width: 750rpx; height: 750rpx;"
    />
    <loading v-if="loadingflag"></loading>
    <Prompt v-model="authSwitch"></Prompt>
  </view>
</template>

<script>
import cardlist from "../../conpoments/cardlist";
import evaluate from "../../conpoments/evaluate";
import reallyInput from "../../conpoments/reallyInput";
import commentList from "../../conpoments/commentList";

import waterfall from "../../conpoments/waterfall";
import Pullcard from "../../conpoments/pullcard";

import {
  inspectHasColl,
  likeArticle,
  delectLike,
  userTalk,
  replyMsg,
  photoArticle,
  recommendNew,
  getCommentlistData,
} from "../../api/api";

export default {
  data() {
    return {
      switchIn: true, //输入框切换
      currentIndex: 0, //当前轮播图选中的索引
      swiperimgArr: [], //顶部轮播图
      list: [], //推荐列表
      reallyValue: "", //真实输入框的值
      offList: false, //控制评论详细列表的弹窗
      safeArea: "", //安全区域高度
      likeArticeValue: 0,
      showLIkeGif: false, //点赞动效开关
      id: "", //文章id
      atCommentId: "", //回复某个评论的id
      allData: {}, //所有数据
      evaluateState: {
        //底部评论数据
        favorite: {},
        commentNum: "", //评论的数量
      },
      swiperHeightArr: [], //所有图片在当前屏幕宽度的等比高度数组
      scw: "", //屏幕宽度 二倍图尺寸
      discussList: [], //所有的评论数据
      index: 0, //评论列表的页码
      size: 10, //每条评论的数量
      hasChart: true, //是否还有数据
      bottom: (32 / 750) * uni.getSystemInfoSync().windowWidth * 2,
      left: (38 / 750) * uni.getSystemInfoSync().windowWidth + "px",
    };
  },
  watch: {
    switchIn: function (newvalue) {
      if (newvalue == true) {
        this.openChartItem = "";
      }
    },
  },
  onShareAppMessage(res) {
    return {
      title: this.allData.shareTitle,
      path: "/pages/photo/photo?id=" + this.id,
      imageUrl: this.allData.shareImage,
      // desc: this.evaluateState.shareSummary,
    };
  },
  async onLoad({ id }) {
    this.id = id;
    this.scw = uni.getSystemInfoSync().windowWidth * 2;
    // this.id=972042687672745984
    try {
      //当前文章所有数据
      this.loadingflag = true;
      let res = await photoArticle(this.id);
      this.loadingflag = false;
      if (res[1].data.code === "000000") {
        this.allData = res[1].data.data;
        const { favorite, commentNum } = res[1].data.data;
        favorite.isFavorite == true
          ? (this.likeArticeValue = 1)
          : (this.likeArticeValue = 0); //是否收藏过
        this.swiperimgArr = this.allData.imageGroups;
        this.evaluateState.favorite = favorite;
        this.evaluateState.favorite.favoriteNumber = Number(
          this.evaluateState.favorite.favoriteNumber
        );
        this.evaluateState.commentNum = commentNum;
        // 组织输入框的数据
      }
    } catch (error) {}
  },
  async onShow() {
    this.safeArea = this.$model;
    
    console.log("onshow");
    try {
      //底部推荐数据
      let res = await recommendNew(this.id);
      if (res[1].data.code === "000000") {
        const { list } = res[1].data;
        this.list = list;
      }
    } catch (error) {}
    this.getcomData(true);
  },
  components: {
    cardlist,
    evaluate,
    reallyInput,
    commentList,
    waterfall,
    Pullcard,
  },
  computed: {
    swiperHeight() {
      if (this.swiperHeightArr.length == this.swiperimgArr.length) {
        //图片全部加载完毕
        let max = 0;
        for (let index = 0; index < this.swiperHeightArr.length; index++) {
          const e = this.swiperHeightArr[index].height;
          e > max ? (max = e) : null;
        }
        return max;
      } else {
        return "";
      }
    },
    previewList() {
      // 文章中部的聊天的三条数据
      if (this.discussList.length > 0) {
        let s = this.discussList.length > 3 ? 3 : this.discussList.length;
        let arr = [];
        for (let index = 0; index < s; index++) {
          arr.push(this.discussList[index]);
        }
        return arr;
      } else {
        return 0;
      }
    },
    color() {
      if (this.list.length > 0) {
        return "#fcfcfc";
      } else {
        return "#fff";
      }
    },
  },
  methods: {
    //轮播图
    swiperChange(e) {
      this.currentIndex = e.target.current;
    },
    //真实输入框切换
    down(flag) {
      this.switchIn = flag;
    },
    openComlist() {
      this.offList = true;
      this.$refs.commentList.startfocus = false;
    },
    async likeArtice(e) {
      if (!this.AuthState()) return;
      this.likeArticeValue = e;
      if (e == 1) {
        this.evaluateState.favorite.favoriteNumber += 1;
        //收藏
        this.showLIkeGif = true;
        setTimeout(() => {
          this.showLIkeGif = false;
        }, 2125);
        let res = await likeArticle(this.id);
        if (res[1].data.code === "000000") {
        }
      } else {
        //取消收藏
        if (Number(this.evaluateState.favorite.favoriteNumber) != 0) {
          this.evaluateState.favorite.favoriteNumber -= 1;
        }
        let res2 = await delectLike(this.id);
        if (res2[1].data.code === "000000") {
          uni.showToast({
            title: "取消收藏",
            duration: 1500,
            icon: "none",
          });
        }
      }
    },
    //输入框提交评论或者回复评论
    async submite(e) {
      if (!this.AuthState()) return;
      let res = await userTalk(this.id, this.reallyValue);
      if (res[1].data.code == "000000") {
        this.switchIn = e;
        this.reallyValue = "";

        this.flashChart();
        uni.showToast({
          title: "评论成功,等待审核",
          duration: 1500,
          icon: "none",
        });
      } else {
        uni.showToast({
          title: "评论失败",
          duration: 1500,
          icon: "none",
        });
      }
    },
    //刷新评论数据
    flashChart() {
      this.index = 0;
      this.discussList = [];
      this.hasChart = true;
      this.getcomData();
    },
    //轮播图的高度数组
    imgload(e, index) {
      const { height, width } = e.detail;
      this.swiperHeightArr.push({
        index: index,
        height: (this.scw * height) / width,
      });
    },
    //图片预览
    preview(item, index) {
      console.log(item);
      let imgarr = this.swiperimgArr.map((v) => v.fileUrl);
      uni.previewImage({
        current: index,
        urls: imgarr,
        indicator: true,
        loop: true,
      });
    },
    async getcomData(first = false) {
      this.index++;
      try {
        let res = await getCommentlistData(this.index, this.size, this.id);
        if (res[1].data.code === "000000") {
          const { content } = res[1].data.data;
          console.log(content);
          if (content != null && content != [] && content.length > 0) {
            this.discussList.push(...(content || []));
          } else {
            this.hasChart = false;
          }
          // this.discussList=
        }
      } catch (error) {}
    },
    //评论列表滚动到底部加载
    scrollBottom() {
      console.log("触底了");
      if (this.hasChart) {
        this.getcomData();
      }
    },
    async checklikestate() {
      let res = await inspectHasColl(this.id);
      console.log(res);
    },
    // //回复别人
    // replayPerson(data) {
    //   data == "" ? (this.switchIn = true) : (this.switchIn = false);
    //   this.openChartItem = data;
    // },
  },
};
</script>

<style lang="scss" scoped>
.photo {
  background-color: #ffffff;
  -webkit-overflow-scrolling: touch;
}

.top-img {
  position: relative;
  width: 100%;
  background-color: #ffffff;
}

.swiper {
  height: 750rpx;
  width: 100%;

  .swiper-item {
    height: 100%;
    width: 100%;
    position: relative;

    .swiper-img {
      width: 100%;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}

.dot {
  width: 96rpx;
  height: 52rpx;
  background: rgba(43, 43, 43, 0.5);
  border-radius: 26rpx;
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
  transform: translateY(50%);
  height: 52rpx;
  > text {
    font-size: 28rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    vertical-align: middle;
  }
}

.article {
  padding-left: 40rpx;
  padding-right: 43rpx;
  padding-top: 45rpx;
  background-color: #ffffff;
  .article-title {
    font-size: 34rpx;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: rgba(36, 36, 36, 1);
    line-height: 48rpx;
  }

  .article-icon {
    margin: 35rpx 0;
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .tuji {
      width: 26rpx;
    }

    > text {
      &:last-child {
        font-size: 24rpx;
        font-family: Source Han Sans CN;
        font-weight: 300;
        color: rgba(117, 117, 117, 1);
        line-height: 38rpx;
        margin-left: 12rpx;
      }
    }
  }

  .article-text {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: 400;
    color: rgba(36, 36, 36, 1);
    line-height: 48rpx;
    text-align: justify;
    letter-spacing: 3rpx;
    word-break: break-word;
  }
}

.dis {
  padding: 0 40rpx;
  padding-top: 50rpx;
  padding-bottom: 64rpx;
  background-color: #ffffff;

  .discuss {
    background: rgba(248, 248, 248, 1);
    border-radius: 10rpx;
    margin: 0 auto;
    padding-left: 30rpx;
    padding-top: 28rpx;
    padding-bottom: 33rpx;

    .speakEvery {
      padding-top: 9rpx;
      font-size: 28rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(117, 117, 117, 1);
    }

    .discuss_list {
      display: flex;
      justify-content: flex-start;
      flex-direction: column;
      align-items: flex-start;

      .discuss_list_margin {
        margin-left: 16rpx;
      }

      > view {
        padding-bottom: 23rpx;

        text {
          &:first-child {
            font-size: 24rpx;
            font-family: PingFang SC;
            font-weight: bold;
            color: rgba(36, 36, 36, 1);
          }

          &:last-child {
            font-size: 24rpx;
            font-family: PingFang SC;
            font-weight: 500;
            color: rgba(117, 117, 117, 1);
          }
        }
      }
    }
  }
}

.recommend {
  border-top: 14rpx solid rgba(248, 248, 248, 1);
}

.recommend-title {
  display: inline-block;
  width: 100%;
  font-size: 28rpx;
  font-family: Source Han Sans CN;
  font-weight: 600;
  color: rgba(36, 36, 36, 1);
  padding-left: 38rpx;
  display: block;
  padding-bottom: 55rpx;
  padding-top: 43rpx;
}

.waterfall_box {
  // padding-top: 32rpx;
  width: 100vw;

  .waterfall_box_cen {
    margin-top: 23rpx;

    image {
      height: auto;
    }
  }
}

.kongTab {
  height: 98rpx;
  margin-top: 2rpx;
}

.toptext {
  width: 100%;
  display: inline-block;
  font-family: PingFang SC;
  color: rgba(36, 36, 36, 1);
  text-align: center;
}
.bgcolorfff {
  background-color: #ffffff;
}
</style>
